package rs.networx.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.NottificationDao;
import rs.networx.dao.PersonDao;
import rs.networx.entity.Person;

//------------------------------------------------------------------------------------------------------------------------------------------------------
public class SendFriendRequestServlet extends HttpServlet {

	private static final long serialVersionUID = -1082855136182224269L;

	@EJB
	private PersonDao personDao;
	
	@EJB
	private NottificationDao nottificationDao;
	
	private String username;
	private String password;
	private Integer intLoggedUserID;
	private Person loggedUser;
	
	private String targetPersonID;
	private Integer intTargetPersonID;
	
	private Integer nottificationsCount;
	
	private boolean pass;
	
	//-------------------------------------------------------------------------------------------------------------------------------------------------
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		System.out.println("LOADED: SendFriendRequestServlet");
		
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());
		
		HttpSession session = request.getSession();
		loggedUser = (Person)session.getAttribute("logovaniUser");
		
		targetPersonID = request.getParameter("personID");
		
		try {	
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
			intLoggedUserID = loggedUser.getId();
			System.out.println(username + "  " + password + "  " + intLoggedUserID);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		
		try {
			intTargetPersonID = Integer.parseInt(targetPersonID);
		} catch (NumberFormatException e) {
			//e.printStackTrace();
		}
		
		// data funkcija personDao.addFriendRequest() dodaje notifikaciju za onoga kome saljemo friend request(target person)... 
		// isto tako, dodaje i notifikaciju za logged usera da je poslao friend request target personu...
		// isto tako, dodaje i po notifikaciju za svakog prijatelja target persona(onoga kome saljemo friend request)...
		// da logged user zeli da bude prijatelj sa njim...
		// isto tako, dodaje i po notifikaciju za svakog prijatelja logged usera(onoga koji salje friend request)...
		// da logged user zeli da bude prijatelj sa target person-om(onaj kome saljemo friend request)...
		pass = personDao.addFriendRequest(intLoggedUserID, intTargetPersonID);
		
		if(pass){
			// treba jos dobaviti i notifikacije za logged usera i okaciti ih na sesiju...
			// sada dobavljamo i notifikacije za datog ulogovanog korisnika ako ih ima...
			nottificationsCount = nottificationDao.getNottificationsCountForLoggedUser(loggedUser);
			System.out.println("nottificationsCount: " + nottificationsCount);
			
			session.setAttribute("nottificationsCount", nottificationsCount);
			
			response.sendRedirect("SearchAgainFromRegisteredUserHomePageServlet");
			return;
		}else if(!pass){
			request.getRequestDispatcher("SearchFromRegistredUserHomePage.jsp").forward(request, response);
			return;
		}
	}
		public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}
	
	
}
